import { request } from '../request';

/** 获取组织列表 */
export const fetchOrganizationList = async () => {
  const data = await request.post<null>('/getOrganizationList');
  return data;
};

/** 获取用户列表
 * @param filterOption - 过滤选项
 */
export const fetchUserList = async (filterOption?: any) => {
  if (filterOption) {
    const { userPhone } = filterOption;
    const data = await request.post<null>('/getUserList', {
      phone: userPhone
    });
    return data;
  }
  const data = await request.post<null>('/getUserList');
  return data;
};

/** 获取供应商列表 */
export const fetchSupplierList = async () => {
  const data = await request.post<null>('/getSupplierList');
  return data;
};

/**
 * 修改密码
 * @param userId - 用户ID
 * @param password - 密码
 * @param newPassword - 新密码
 */
export function fetchChangePassword(userId: string, password: string, newPassword: string) {
  return request.post<null>('/changePassword', {
    userId,
    password,
    newPassword
  });
}

/**
 * 添加/编辑用户
 * @param formModel - 用户信息表单
 */
export function fetchEditUser(formModel: any) {
  const { userId, userName, password, phone, supplierId, organizationId, userStatus } = formModel;
  if (password) {
    return request.post<null>('/editUser', {
      userId: userId.toString(),
      phone: phone.toString(),
      userName: userName.toString(),
      password: password.toString(),
      supplierId: supplierId.toString(),
      organizationId: organizationId.toString(),
      userStatus: userStatus.toString()
    });
  }
  return request.post<null>('/editUser', {
    userId: userId.toString(),
    phone: phone.toString(),
    userName: userName.toString(),
    supplierId: supplierId.toString(),
    organizationId: organizationId.toString(),
    userStatus: userStatus.toString()
  });
}

/**
 * 删除用户
 * @param userId - 用户ID
 */
export function fetchDeleteUser(userId: string) {
  return request.post<null>('/deleteUser', {
    userId
  });
}
